<template>
  <div class="app-container">
    <div class="search-line">
      <el-breadcrumb separator="/" style="margin-bottom: 10px;">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: 'expression' }">表情包管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-input v-model="searchVal" placeholder="请输入搜索内容" clearable style="width: 150px;margin-right: 10px;"></el-input>
      <el-select filterable v-model="changeValue" placeholder="请选择" style="width: 150px;margin-right: 10px;">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <cus-wraper>
      <cus-filter-wraper>
        <el-button type="danger" @click="toggleSelection" icon="el-icon-search" style="margin-left: 15px;">批量删除</el-button>
        <el-button type="primary" @click="handleCreate" icon="el-icon-plus">添加</el-button>
      </cus-filter-wraper>
      <div class="table-container">
        <el-table v-loading="listLoading" :data="list" ref="itsmDataTable" size="mini" fit element-loading-text="Loading"
                  highlight-current-row align="center">
          <el-table-column type="selection" width="45"></el-table-column>
          <el-table-column label="序号" prop="id" align="center" width="65"></el-table-column>
          <el-table-column label="名称" prop="expressionName" align="center" width="105"></el-table-column>
          <el-table-column label="图片" prop="expressionImg" align="center" width="85">
            <template slot-scope="scope">
              <span>
                <img :src="scope.row.expressionImg" alt="" style="width: 50px;">
              </span>
            </template>
          </el-table-column>
          <el-table-column label="表情" prop="expressionAnimation" align="center" width="150%">
            <template slot-scope="scope">
              <span>
                <img :src="scope.row.expressionAnimation" alt="" style="width: 50px;">
              </span>
            </template>
          </el-table-column>
<!--          <el-table-column label="排序" prop="id" align="center"></el-table-column>-->
          <el-table-column align="center" v-if="this.global_checkBtnPermission(['sys:user:edit','sys:user:delete'])" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="handleUpdate(scope.row)" icon="el-icon-edit" plain>
                编辑
              </el-button>
              <cus-del-btn  @ok="handleDelete(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"/>
      </div>

      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogVisible" width="40%" v-dialogDrag @close="handleDialogClose">
        <el-form ref="dataForm" :model="form" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item label="表情名称:" prop="username">
            <el-input v-model="form.expressionName"></el-input>
          </el-form-item>
          <el-form-item label="表情图片:" prop="username">
            <el-upload
              action=""
              ref="upload"
              :http-request="Upload"
              :auto-upload="true"
              list-type="picture-card"
              :on-change="handleLimit"
              :on-remove="handleRemove"
              :limit="1"
              :class="{disabled:uploadDisabled}"
              :style="{display:form.expressionImg?'none':'block'}"
            >
              <i class="el-icon-plus" />
            </el-upload>
            <img class="uploadImg" :src="form.expressionImg" :style="{display:form.expressionImg?'block':'none'}" alt="">
            <el-button type="primary" @click="reload(1)" class="reload" :style="{display:form.expressionImg?'block':'none'}">重新上传</el-button>
          </el-form-item>
          <el-form-item label="表情动态图:" prop="username">
            <el-upload
              action=""
              ref="upload2"
              :http-request="Upload2"
              :auto-upload="true"
              list-type="picture-card"
              :on-change="handleLimit2"
              :on-remove="handleRemove2"
              :limit="1"
              :class="{disabled2:uploadDisabled2}"
              :style="{display:form.expressionAnimation?'none':'block'}"
            >
              <i class="el-icon-plus" />
            </el-upload>
            <img class="uploadImg2" :src="form.expressionAnimation" :style="{display:form.expressionAnimation?'block':'none'}" alt="">
            <el-button type="primary" @click="reload(2)" class="reload" :style="{display:form.expressionAnimation?'block':'none'}">重新上传</el-button>
          </el-form-item>
<!--          <el-form-item label="排序:" prop="username">-->
<!--            <el-input v-model="form.username"></el-input>-->
<!--          </el-form-item>-->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false"> 取 消 </el-button>
          <el-button type="primary" @click="submitForm"> 确 认 </el-button>
        </span>
      </el-dialog>
    </cus-wraper>
  </div>
</template>

<script>
  import { getSysUserPage, saveSysUser, deleteSysUser } from '@/api/dictionaries/expression'
  import { client } from '@/utils/ali-oss'

  export default {
    data() {
      return {
        dialogVisible: false,
        list: [],
        listLoading: true,
        total: 0,
        listQuery: {
          // id:7,
          // expressionName
          page: 1,
          pageSize: 10,
          username: undefined
        },
        form: {
          id: undefined, //主键ID
          expressionName: undefined, //账号
          expressionImg: undefined, //账号
          expressionAnimation: undefined, //账号
        },
        dialogStatus: '',
        titleMap: {
          update: '编辑表情',
          create: '创建表情'
        },

        dialogVisibleImg1: false,
        uploadDisabled:false,
        dialogVisibleImg2: false,
        uploadDisabled2:false,
        options: [{
          value: '1',
          label: '序号'
        }, {
          value: '2',
          label: '名称'
        }],
        changeValue: '1',
        searchVal: '',

        rules: {
          expressionName: [
            {required: true, message: '请输入表情名称', trigger: 'blur'},
          ],
          expressionImg: [
            {required: true, message: '请上传表情图片', trigger: 'blur'},
          ],
          expressionAnimation: [
            {required: true, message: '请上传表情动态图', trigger: 'blur'},
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true;
        getSysUserPage(this.listQuery).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
        })
      },
      search(){
        if(this.changeValue === '1'){
          this.listQuery.id = this.searchVal;
          this.listQuery.expressionName = '';
        }else{
          this.listQuery.expressionName = this.searchVal;
          this.listQuery.id = '';
        }

        this.getList();
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create';
        this.dialogVisible = true
      },
      handleUpdate(row) {
        // row)
        this.form = Object.assign({}, row);
        this.dialogStatus = 'update';
        this.dialogVisible = true
      },
      handleDelete(row) {
        let id = [ row.id ]
        deleteSysUser(id).then(response => {
          if (response.code === 0) {
            this.getList();
            this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
          }
        })
      },
      submitForm() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            saveSysUser(this.form).then(response => {
              if (response.code === 0) {
                this.getList();
                this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });;
                this.dialogVisible = false
              } else {
                this.$message({
                message: response.message,
                type: 'error'
            });
              }
            }).catch(err => {

            })
          }
        })
      },
      resetForm() {
        this.form = {
          id: undefined, //主键ID
          expressionName: undefined, //账号
          expressionImg: undefined, //账号
          expressionAnimation: undefined, //账号
        }
        if(this.$refs.upload){
          this.$refs.upload.clearFiles();
        }
        if(this.$refs.upload2){
          this.$refs.upload2.clearFiles();
        }
        this.uploadDisabled = false;
        this.uploadDisabled2 = false;
      },
      // 监听dialog关闭时的处理事件
      handleDialogClose() {
        this.resetForm();

        if (this.$refs['dataForm']) {
          this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
        }
      },
      toggleSelection(){
        // 一键删除
        const _selectData = this.$refs.itsmDataTable.selection;
        if(_selectData.length === 0){return}
        let selectArry = [];
        _selectData.map(row => {
          selectArry.push(row.id)
        });

        deleteSysUser(selectArry).then(response => {
          if (response.code === 0) {
            this.getList();
            this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
          }
        })
      },

      // 上传图片
      Upload(file) {
        const that = this;
        const names = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
        if (!names.includes(file.file.type)) {this.$message.error('不支持的格式!'); return }
        const fileName = Date.now() + '-' + file.file.name;
        client().put(fileName, file.file,).then(result => {
            that.form.expressionImg = result.url
          }).catch(err => {

        })
      },
      // 删除图片
      handleRemove(fileList) {
        this.form.expressionImg = '';
        this.uploadDisabled = fileList.length>=1
      },
      handleLimit(file,fileList){
        this.uploadDisabled = fileList.length>=1
      },

      // 上传图片
      Upload2(file) {
        const that = this;
        const names = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
        if (!names.includes(file.file.type)) {this.$message.error('不支持的格式!'); return }
        const fileName = Date.now() + '-' + file.file.name;
        client().put(fileName, file.file,).then(result => {
          that.form.expressionAnimation = result.url
        }).catch(err => {

        })
      },
      // 删除图片
      handleRemove2(fileList) {
        this.form.expressionAnimation = '';
        this.uploadDisabled2 = fileList.length>=1
      },
      handleLimit2(file,fileList){
        this.uploadDisabled2 = fileList.length>=1
      },
      reload(val){
        if (val===1){
          this.form.expressionImg = '';
          this.uploadDisabled = false;
          this.$refs.upload.clearFiles();
        }else{
          this.form.expressionAnimation = '';
          this.uploadDisabled2 = false;
          this.$refs.upload2.clearFiles();
        }
      }

    }
  }
</script>
<style lang="scss" scoped>
  // .el-upload--picture-card 控制加号部分
  .disabled /deep/ .el-upload--picture-card {
    display: none;
  }
  .disabled2 /deep/ .el-upload--picture-card {
    display: none;
  }
  .uploadImg, .uploadImg2{
    width:148px;
    height: 148px;
    border-radius: 6px;
  }
  .reload{
    position: absolute;
    bottom: 0;
    left: 160px;
  }
  .search-line{
    margin-bottom: 10px;
    padding: 10px;
    background: white;
  }
  .filter-wraper{
    display: flex;
    flex-direction: row-reverse;
  }
  .table-container /deep/ .el-table .cell{
    display:flex;
  }
</style>
